package CustomUI;

import GameTools.ydPoint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class ydColorLineMinor extends View {

	ydPoint size;
	float r=255;
	float b=0;
	float g=0;
	
	float delta=0;
	
	int sections=0;
	
	Paint paint= new Paint();
	
	ydOnTouchListeners listeners = new ydOnTouchListeners();
	
	private Paint mPaint;
	private float mCurrentHue = 0;
	private int mCurrentX = 0, mCurrentY = 0;
	private int mCurrentColor, mDefaultColor;
	private final int[] mHueBarColors = new int[258];
	private int[] mMainColors = new int[65536];
	public Canvas mCanvas;
	
	public ydColorLineMinor(Context context) {
		super(context);
		init();	
	}

	public ydColorLineMinor(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ydColorLineMinor(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public void init(){
		this.size= new ydPoint(50,255);
		int color = Color.BLUE;
		float[] hsv = new float[3];
		Color.colorToHSV(color, hsv);
		mCurrentHue = hsv[0];
		
		mCurrentColor = color;

		// Initialize the colors of the hue slider bar
		int index = 0;
		for (float i=0; i<256; i += 256/42) // Red (#f00) to pink (#f0f)
		{
			mHueBarColors[index] = Color.rgb(255, 0, (int) i);
			index++;
		}
		for (float i=0; i<256; i += 256/42) // Pink (#f0f) to blue (#00f)
		{
			mHueBarColors[index] = Color.rgb(255-(int) i, 0, 255);
			index++;
		}
		for (float i=0; i<256; i += 256/42) // Blue (#00f) to light blue (#0ff)
		{
			mHueBarColors[index] = Color.rgb(0, (int) i, 255);
			index++;
		}
		for (float i=0; i<256; i += 256/42) // Light blue (#0ff) to green (#0f0)
		{
			mHueBarColors[index] = Color.rgb(0, 255, 255-(int) i);
			index++;
		}
		for (float i=0; i<256; i += 256/42) // Green (#0f0) to yellow (#ff0)
		{
			mHueBarColors[index] = Color.rgb((int) i, 255, 0);
			index++;
		}
		for (float i=0; i<256; i += 256/42) // Yellow (#ff0) to red (#f00)
		{
			mHueBarColors[index] = Color.rgb(255, 255-(int) i, 0);
			index++;
		}

		// Initializes the Paint that will draw the View
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setTextAlign(Paint.Align.CENTER);
		mPaint.setTextSize(12);
		setSize(new ydPoint(255, 255));
		
		listeners.addOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				float x = event.getX();
				float y = event.getY();


					// Update the main field colors
					mCurrentHue = (255-y)*360/255;

					// Update the current selected color
					int transX = mCurrentX-10;
					int transY = mCurrentY-60;
					int index = 256*(transY-1)+transX;


					// Force the redraw of the dialog
					invalidate();

			return true;
			}
		});
		
		this.setOnTouchListener(listeners);
		
	}
	
	public void setSize(ydPoint size){
		this.size= new ydPoint(size);
		
		delta=(255)/size.y;
		invalidate();
		return;
	}
	
	Path path=new Path();

	@Override
	public void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		mCanvas=canvas;
		int translatedHue = 255-(int)(mCurrentHue*255/360);
		
	    // Display all the colors of the hue bar with lines
	
	    for (int x=0; x<size.y; x++)
	
	    {
	
	        // If this is not the current selected hue, display the actual color
	
	        if (translatedHue != x)
	
	        {
	
	            mPaint.setColor(mHueBarColors[(int) (delta*x)]);
	
	            mPaint.setStrokeWidth(1);
	
	        }
	
	        else // else display a slightly larger black line
	
	        {
	
	            mPaint.setColor(Color.BLACK);
	
	            mPaint.setStrokeWidth(3);
	
	        }
	
	        canvas.drawLine(0, x, size.x, x, mPaint);
	
	    }
	    
	    path.reset();
		path.moveTo(0, 0);
		path.addRect(0,0, 40,255, Direction.CCW);
		paint.setColor(Color.WHITE);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(3);
		 canvas.drawPath(path, paint);
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
			float x = event.getX();
			float y = event.getY();


				// Update the main field colors
				mCurrentHue = (255-y)*360/255;

				// Update the current selected color
				int transX = mCurrentX-10;
				int transY = mCurrentY-60;
				int index = 256*(transY-1)+transX;


				// Force the redraw of the dialog
				invalidate();

		return true;
	}

	public float getCurrentHue() {
		// TODO Auto-generated method stub
		return mCurrentHue;
	}

	public void setHue(int i) {
		mCurrentHue = (255-i)*360/255;
		int transX = mCurrentX-10;
		int transY = mCurrentY-60;
		int index = 256*(transY-1)+transX;


		// Force the redraw of the dialog
		//invalidate();
	}


}
